<?php
  function getIssue() {
    $id = $_POST['id'];
    if ($id == null) $sql = "SELECT * FROM `issue_list` order by id desc limit 8";
    else $sql = "SELECT * FROM `issue_list` where `id` < '$id' order by id desc limit 8";
    $query = mysql_query($sql);
    while ($rs = mysql_fetch_array($query)) {
      $_id = $rs['owner'];
      $_sql = "SELECT * FROM `user` where `id` = $_id";
      $_query = mysql_query($_sql);
      while ($_rs = mysql_fetch_array($_query)) $owner = array(
        'id' => $_rs['id'],
        // 'name' => $_rs['name'],
        'nick' => $_rs['nick'],
        // 'gender' => $_rs['gender'],
        // 'phone' => $_rs['phone'],
        // 'contact' => $_rs['contact'],
        // 'validity' => $_rs['validity'],
        // 'last_login' => $_rs['last_login'],
        // 'date' => $_rs['date'],
        // 'city' => $_rs['city'],
        // 'language' => $_rs['language'],
        // 'introduction' => $_rs['introduction'],
        'avatar' => $_rs['avatar'],
      );
      $issue[] = array(
        'id' => $rs['id'],
        'owner' => $owner,
        'title' => $rs['title'],
        'text' => $rs['text'],
        'image' => $rs['image'],
        'likes' => $rs['likes'],
        'reviews' => $rs['reviews'],
        'visits' => $rs['visits'],
        'validity' => $rs['validity'],
        'time' => $rs['time'],
        'thumb' => $rs['thumb'],
      );
    }
    print_r(json_encode($issue));
    mysql_close($link);
  }

  function submit() {
    $title = $_POST['title'];
    $text = $_POST['text'];
    $time = $_POST['time'];
    $owner = $_POST['owner'];
    $imageNumber = $_POST['imageNumber'];
    $image = json_decode($_POST['image']);
    // $image = array();
    // $image = $_POST['image'];

    $sql = "INSERT INTO `issue_list` (`title`, `text`, `time`, `owner`, `image`) VALUES ('{$title}', '{$text}', '{$time}', '{$owner}', '{$imageNumber}');";
    $query = mysql_query($sql);
    $sql = "SELECT * FROM `issue_list` where `owner` = '$owner' order by time desc limit 1";
    $query = mysql_query($sql);
    while ($rs = mysql_fetch_array($query)) $issue = array(
      'id' => $rs['id'],
      'title' => $rs['title'],
      'text' => $rs['text'],
      'imageNumber' => $rs['imageNumber'],
      'likes' => $rs['likes'],
      'reviews' => $rs['reviews'],
      'visits' => $rs['visits'],
      'validity' => $rs['validity'],
      'time' => $rs['time'],
      'thumb' => $rs['thumb'],
    );
    print_r(json_encode($issue));

    $id = $issue['id'];
    for ($i = 0; $i < $imageNumber; $i++) {
      $sql = "INSERT INTO `issue_image` (`issue`, `time`, `image`) VALUES ('{$id}', '{$time}', '{$image[$i]}');";
      $query = mysql_query($sql);
    }
    mysql_close($link);
  }
?>